<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 附件控制器
 * 
 * @version 1.0.0 
 * @author  Jason
 * @date	2017年5月22日 下午2:46:42
 * 
 * @desc    完善文档注释
 * @version 1.1.0
 * @date	2017年6月27日 下午5:16:26
 * 
 * @title 附件管理
 * @entry http://discovery.segway.com
 */
class Attachment extends MY_Business_Controller
{
	// Must be public!!!
	/** @var Lib_attachment */
	public $lib_attachment = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('lib_attachment');
// 		$this->lang->load('attachment');
	}
	
	/**
	 * 上传文件
	 * 
	 * @desc    上传文件
	 * @method  POST
	 * @author  Jason
	 * @date	2017年5月22日 下午2:49:29
	 * @param   $file	文件域
	 * @return  JSON	标准JSON格式响应
	 * 					结构：/business/attachment/test_upload_form
	 * 
	 * @link    /business/attachment/ajax_upload_image
	 * 
	 * @structUrl <entry>/business/attachment/test_upload_form
	 */
	public function ajax_upload_image()
	{
		list($status, $content) = $this->lib_attachment->qiniu_upload('file', 'image');
		
		if(!$status) {
			$messageContent = Lib_json::getMessageContent($content);
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content), $messageContent);
		} else {
			Lib_json::renderSuccess([
					'image_hash' => $content['hash'],
					'image_key' => $content['key'],
					'image_url' => $content['url'],
			]);
		}
	}
	
	/**
	 * 文件上传测试表单
	 * 
	 * @desc    文件上传测试表单
	 * @method  GET
	 * @author  Jason
	 * @date	2017年5月22日 下午2:49:29
	 * @return  html form
	 * 
	 * @link    /business/attachment/test_upload_form
	 * 
	 * @command pass
	 */
	public function test_upload_form()
	{
		$url = "/business/attachment/ajax_upload_image?debug=1";
		echo "<form method='post' action='$url' enctype='multipart/form-data'>";
		echo "<input name='file' type='file' />";
		echo "<button>Upload</button>";
		echo "</form>";
	}
	
}